<?
class Vista
{


	var $negozio;
	var $db;
	
	var $template;
	var $carrello;
	var $ordine;

	// VARIABILI DI SOSTITUZIONE TEMPLATE PRINCIPALE
	var $title;
	var $titolo;
	var $menu_principale;
	var $menu_contesto;
	var $messaggio;
	var $contenuto_pagina;
	var $box_carrello;
	//

	function Vista(&$negozio, &$db)
	{

		$this->negozio = &$negozio;
		$this->carrello = &$negozio->carrello;
		$this->ordine = &$negozio->ordine;
		$this->db = &$db;

		$this->template = $_SERVER[HTTP_HOST].'/template/template.php';		
		if(!file_exists($this->template)) $this->template = '_default/template/template.php';
	}

	function home()
	{
		global $lang;
		ob_start();
		$file_tpl = $_SERVER[HTTP_HOST].'/template/home_page.php';
		if(!file_exists($file_tpl)) $file_tpl = '_default/template/home_page.php';
		include($file_tpl);
		$this->contenuto_pagina = ob_get_contents();
		ob_end_clean();
	}

	function prodotto($prodotto)
	{
		global $lang;
		//echo "##".$prodotto->id;
		ob_start();
		$file_tpl = $_SERVER[HTTP_HOST].'/template/scheda_prodotto.php';
		if(!file_exists($file_tpl)) $file_tpl = '_default/template/scheda_prodotto.php';
		include($file_tpl);
		$this->contenuto_pagina = ob_get_contents();
		ob_end_clean();

	}

	function categoria($categoria)
	{
		global $lang;
		//echo "##".$categoria->id;
		ob_start();
		$file_tpl = $_SERVER[HTTP_HOST].'/template/lista_prodotti.php';
		if(!file_exists($file_tpl)) $file_tpl = '_default/template/lista_prodotti.php';
		include($file_tpl);
		$this->contenuto_pagina = ob_get_contents();
		ob_end_clean();
	}
	
	function chiuso()
	{
		global $lang;
		//echo "##".$categoria->id;
		ob_start();
		$file_tpl = $_SERVER[HTTP_HOST].'/template/chiuso.php';
		if(!file_exists($file_tpl)) $file_tpl = '_default/template/chiuso.php';
		include($file_tpl);
		$this->contenuto_pagina = ob_get_contents();
		ob_end_clean();
	}

	function cerca($q)
	{
		global $lang;
	}
	function info($q)
	{
		global $lang;
		//echo "##".$categoria->id;
		ob_start();
		$file_tpl = $_SERVER[HTTP_HOST].'/template/info.php';
		if(!file_exists($file_tpl)) $file_tpl = '_default/template/info.php';
		include($file_tpl);
		$this->contenuto_pagina = ob_get_contents();
		ob_end_clean();
	}
	function carrello()
	{
		global $lang;

		$contenuto = $this->carrello->get_carrello();

		ob_start();
		$file_tpl = $_SERVER[HTTP_HOST].'/template/carrello.php';
		if(!file_exists($file_tpl)) $file_tpl = '_default/template/carrello.php';
		include($file_tpl);
		$this->contenuto_pagina = ob_get_contents();
		ob_end_clean();
	}
	
	function ordine()
	{
		global $lang;		
		
		$ord = $this->negozio->ordine->ord;
		$sped = $this->negozio->get_metodi_spedizione();
		//print_r($sped);
		$pag = $this->negozio->get_metodi_pagamento();
		ob_start();
		$file_tpl = $_SERVER[HTTP_HOST].'/template/ordine_'.$this->ordine->status.'.php';
		if(!file_exists($file_tpl)) $file_tpl = '_default/template/ordine_'.$this->ordine->status.'.php';
		include($file_tpl);
		$this->contenuto_pagina = ob_get_contents();
		ob_end_clean();
	}

	function display()
	{
		global $lang;
		$title = 'Titolo della pagina';
		$pagina = '<form id="form1" name="form1" method="post" action="">
		  <label>
		  <input type="text" name="textfield" />
		  </label>
		  <label>Invia
		  <input type="submit" name="Submit" value="Invia" />
		  </label>
		</form>';
		$menu = 'Menu delle categorie';

		// includo menu principale
		$this->menu_principale();
		$this->menu_contesto();
		if($this->negozio->messaggio||$this->carrello->messaggio||$this->ordine->messaggio)$this->messaggio();
		$this->box_carrello();

		ob_start();
		include($this->template);
		$html = ob_get_contents();
		ob_end_clean();
		return $html;
	}

	function menu_principale()
	{
		global $lang;
		$albero = $GLOBALS[cat_struct][albero][0];
		ob_start();
		$file_tpl = $_SERVER[HTTP_HOST].'/template/menu_principale.php';
		if(!file_exists($file_tpl)) $file_tpl = '_default/template/menu_principale.php';
		include($file_tpl);
		$this->menu_principale = ob_get_contents();
		ob_end_clean();
	}

	function menu_contesto()
	{
		global $lang;
		$albero = $this->negozio->contesto;
		//print_r($albero);
		$categorie = $GLOBALS[cat_struct][categorie];
		// SOLO SE NON SONO IN PRODOTTO TROVO CATEGORIA + PROFONDA
		if(!$this->negozio->risorsa[$_REQUEST[prodotto]])
		foreach($albero as $questa_categoria=>$questa_categoria_dir);
		//print_r($albero);


		ob_start();
		$file_tpl = $_SERVER[HTTP_HOST].'/template/menu_contesto.php';
		if(!file_exists($file_tpl)) $file_tpl = '_default/template/menu_contesto.php';
		include($file_tpl);
		$this->menu_contesto = ob_get_contents();
		ob_end_clean();
	}
	
	function messaggio()
	{
		global $lang;
		ob_start();
		$file_tpl = $_SERVER[HTTP_HOST].'/template/messaggio.php';
		if(!file_exists($file_tpl)) $file_tpl = '_default/template/messaggio.php';
		include($file_tpl);
		$this->messaggio = ob_get_contents();
		ob_end_clean();
	}

	function box_carrello()
	{
		global $lang;
		ob_start();
		$file_tpl = $_SERVER[HTTP_HOST].'/template/box_carrello.php';
		if(!file_exists($file_tpl)) $file_tpl = '_default/template/box_carrello.php';
		include($file_tpl);
		$this->box_carrello = ob_get_contents();
		ob_end_clean();
	}



	/* UTILITY*/
	function Truncate($string, $limit, $break=" ", $pad="...") {
		if(strlen($string) <= $limit) return $string;
		if(false !== ($breakpoint = strpos($string, $break, $limit))) { 
			if($breakpoint < strlen($string) - 1) { 
				$string = substr($string, 0, $breakpoint) . $pad; 
			} 
		} return $string; 
	}

}

?>